<template>
  <div class="base_info">
    <div class="title">发布面试经验</div>
    <div class="content">
      <header>面试基本信息填写:</header>
      <el-form :model="baseInfoForm"
               :rules="rules"
               ref="baseInfoForm"
               label-width="80px"
               class="baseInfoForm">
        <el-form-item label="公司名称" prop="firm">
          <el-input v-model="baseInfoForm.firm"
                    placeholder="请输入公司名称"></el-input>
        </el-form-item>

        <el-form-item label="所在城市" prop="city">
          <el-autocomplete
            class="inline-input"
            v-model="baseInfoForm.city"
            :fetch-suggestions="querySearch"
            placeholder="请输入所在城市"
            :trigger-on-focus="false"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>

        <el-form-item label="面试职位" prop="position">
          <el-input v-model="baseInfoForm.position"
                    placeholder="请输入面试职位"></el-input>
        </el-form-item>

        <div class="write_interview">
          <el-form-item>
            <el-button type="primary" @click="submitForm('baseInfoForm')">填好了，去撰写面经</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {routerReplace} from "common/Utils";
  import {city} from "common/Utils";

  export default {
    name: "BaseInfo",
    data() {
      return {
        restaurants: [],
        baseInfoForm: {
          firm: '',
          city: '',
          position: ''
        },
        rules: {
          firm: [
            { required: true, message: '请输入公司名称', trigger: 'blur' }
          ],
          city: [
            {required: true, message: '请输入所在城市', trigger: 'blur'}
          ],
          position: [
            {required: true, message: '请输入面试职位', trigger: 'blur'}
          ]
        }
      };
    },
    mounted() {
      this.restaurants = this.loadAll();
    },
    methods: {
      querySearch(queryString, cb) {
        let restaurants = this.restaurants;
        let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      loadAll() {
        return city()
      },
      handleSelect(item) {
        console.log(item);
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$router.push({
              path: '/publish/detail',
              name: 'detail', // 传递参数的时候需要传递name
              params: {
                baseInfo: this.baseInfoForm
              }
            }, )
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    },
  }
</script>

<style scoped>
  .base_info {
    height: calc(100vh - 64px);
    background-color: #fff;
  }

  .title {
    padding: 40px 0;
    line-height: 60px;
    font-size: 24px;
    font-weight: 700;
    text-align: center;
  }

  .content {
    width: 400px;
    margin: 10px auto;
  }

  .content header {
    padding-bottom: 35px;
    font-size: 18px;
    font-weight: 700;
  }

  .baseInfoForm >>> .el-autocomplete {
    width: 320px;
  }

  .el-form-item {
    margin-bottom: 25px;
  }

  .write_interview {
    margin-left: 35px;
  }

  .el-button--primary {
    background-color: #4361a7;
    border-color: #4361a7;
    color: #fff;
  }

  .el-button--primary:hover {
    background-color: #5177ce;
    border-color: #5177ce;
    color: #fff;
  }

</style>
